package com.tangsm.springboot.redisson.demo.controller;

import cn.hutool.core.date.DateUtil;
import com.tangsm.springboot.redisson.demo.domain.Order;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/queue")
public class BlockingQueueController {

    AtomicInteger i = new AtomicInteger(0);

    @Autowired
    RedissonClient redissonClient;

    private static final int DELAYTIME = 1 * 60;   //延迟时间，1分钟

    @GetMapping("/test")
    public String test(){
        RBlockingQueue<Order> blockingQueue = redissonClient.getBlockingQueue("delay_queue");
        RDelayedQueue<Order> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);

        for (int orderIndex = 0; orderIndex <= 99; orderIndex++) {
            Order order = new Order();
            order.setOrderNo(String.format("order%02d-test-%d", orderIndex, 1 + i.get()));
            order.setCreateDate(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss:SSS"));
            delayedQueue.offer(order, DELAYTIME, TimeUnit.SECONDS);   //放入队列
        }
        i.incrementAndGet();
        return "成功：" + i.get();
    }
}
